package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.pojo.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController //@ResponseBody + @Controller的组合注解
@RequestMapping("/shop") //暴露一个公共的路径
@Api(tags = "店铺管理接口",description = "店铺管理接口描述")
public class ShopController {

    /**
     * 注入对象
     */
    @Autowired
    private IShopService shopService;

    /**
     * 分页查询
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询所有店铺接口",notes = "分页查询店铺列表")
    public PageList<Shop> findPage(@RequestBody ShopQuery query){

        //query ，高级查询的参数  万能键alt+enter
        return shopService.findPage(query);
    }


    /**
     * 查询所有
     * @RequestMapping(value = "/findAll",method = RequestMethod.GET)
     */
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有店铺接口",notes = "店铺列表")
    public AjaxResult findAll(){

        return AjaxResult.createSuccess(shopService.findAll());
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        //店铺入驻
        shopService.settlement(shop);

        return AjaxResult.createSuccess();
    }

    //http://localhost/shop/active/5b2b0ee1bf654fba82e5fd8f9885808d
    /**
     * 店铺激活
     * @param shop
     * @return
     *
     * https://tupi-demo.oss-cn-hangzhou.aliyuncs.com/1672130217696.gif
     */
    @GetMapping("/active/{uuid}")
    public AjaxResult settlement(@PathVariable("uuid") String uuid){
        //店铺入驻
        return shopService.active(uuid);
    }


    /**
     * 新增或者修改，没有id就是新增
     * @param shop
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改店铺",notes = "没有id就是新增")
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            shopService.saveOrUpdate(shop);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }

    /**
     * 根据id删除一个对象
     * @return
     * localhost/dept/10
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除店铺",notes = "根据id删除一个对象")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }
    }


}
